using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.PlayerLoop;
public enum ArrowType
{
    normal,
    skill1,
    skill2,
    skill3
}
public class Arrow : MonoBehaviour
{
    private RaycastHit hitInfo;
    private Vector3 target;
    public float speed;
    public ArrowType arrowType;
    public GameObject Boom;
    private AttackData_SO attackData;


    private void Awake()
    {
        attackData = GameManager.Instance.playerStats.attackData;
        target = FindObjectOfType<ArcherController>().GetComponent<ArcherController>().targetPos;
        if (arrowType == ArrowType.skill2||arrowType==ArrowType.skill3)
        {
            transform.Rotate(-90, 0, 0);
        }

        Destroy(this.gameObject,2f);
    }

    private void Update()
    {
        if (transform.gameObject&&arrowType!=ArrowType.skill3)
        {
            transform.DOMove(target, speed * Time.deltaTime);
        }
        else if (transform.gameObject && arrowType == ArrowType.skill3)
        {
            transform.DOMove(transform.position-transform.up.normalized*10, speed * Time.deltaTime);
        }
    }


    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {
            if (arrowType == ArrowType.normal)
            {
                var collisionStats = other.gameObject.GetComponent<CharacterStats>();
                collisionStats.TakeDamage((int)(attackData.maxDamage), collisionStats);
                Destroy(this.gameObject);
            }
            else if (arrowType == ArrowType.skill1)
            {
                var collisionStats = other.gameObject.GetComponent<CharacterStats>();
                collisionStats.TakeDamage((int)(attackData.maxDamage*1.3f), collisionStats);
                Instantiate(Boom, this.transform.position, Quaternion.identity);
                Destroy(this.gameObject);
            }
            else if (arrowType == ArrowType.skill2)
            {
                var collisionStats = other.gameObject.GetComponent<CharacterStats>();
                collisionStats.TakeDamage((int)(attackData.maxDamage*1.2f), collisionStats);
                Destroy(this.gameObject);
            }
            else if (arrowType == ArrowType.skill3)
            {
                var collisionStats = other.gameObject.GetComponent<CharacterStats>();
                collisionStats.TakeDamage((int)(attackData.maxDamage*1.5f), collisionStats);
                Destroy(this.gameObject);
            }
        }
    }
}
